package com.jinlejia.erp.ui.new_house.apply;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.freedom.yefeng.yfrecyclerview.YfListInterface;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.MyViewPagerAdapter;
import com.jinlejia.erp.databinding.FragmentApplyBinding;
import com.jinlejia.erp.ui.common.SelectDialog;
import com.jinlejia.erp.ui.new_house.recommend.RecommendClientsActivity;
import com.jinlejia.erp.ui.new_house.recommend.RecommendValidActivity;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class ApplyFragment extends Fragment {


    private View view;

    private String[] mTitles = {"全部", "推荐", "带访", "认购", "成交"};
    // 填充到ViewPager中的Fragment
    private List<Fragment> mFragments;
    // ViewPager的数据适配器
    private MyViewPagerAdapter mViewPagerAdapter;

    public ApplyFragment() {
        // Required empty public constructor
    }

    private FragmentApplyBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_apply, null);
            binding = DataBindingUtil.bind(view);
            binding.idBack.setOnClickListener(this::onViewClicked);
            binding.addApply.setOnClickListener(this::onViewClicked);
            initView();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        binding = DataBindingUtil.bind(view);
        binding.idBack.setOnClickListener(this::onViewClicked);
        binding.addApply.setOnClickListener(this::onViewClicked);
        return view;
    }

    private void initView() {

        mFragments = new ArrayList<>();
        mFragments.add(0, ApplyListFragment.newInstance("-1,0,1,2,3,4,5,6,7,8"));
        mFragments.add(1, ApplyListFragment.newInstance("0,1,2"));
        mFragments.add(2, ApplyListFragment.newInstance("3,4,5"));
        mFragments.add(3, ApplyListFragment.newInstance("6"));
        mFragments.add(4, ApplyListFragment.newInstance("7"));

        // 初始化ViewPager的适配器，并设置给它
        mViewPagerAdapter = new MyViewPagerAdapter(getFragmentManager(), mTitles, mFragments);
        binding.idViewpager.setAdapter(mViewPagerAdapter);

        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        binding.idTablayout.setTabsFromPagerAdapter(mViewPagerAdapter);
        binding.idTablayout.post(() -> {
            // 将TabLayout和ViewPager进行关联，让两者联动起来
            binding.idTablayout.setupWithViewPager(binding.idViewpager);
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_back:
                EventBus.getDefault().post("close", "TAG_FINISH_NEW_HOUSE");
                break;
            case R.id.add_apply:

                ArrayList<String> data = new ArrayList<>();
                data.add("推荐申请");
                data.add("带访申请");
                data.add("复访申请");
                final SelectDialog dialog = new SelectDialog(getActivity(), "选择申请类型", data);
                dialog.show();
                dialog.adapter.setOnItemClickListener(new YfListInterface.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, Object o) {
                        dialog.dismiss();
                        if (o.equals("推荐申请")) {
                            startActivity(new Intent(getActivity(), RecommendClientsActivity.class));
                        } else if (o.equals("带访申请") || o.equals("复访申请")) {
                            Intent intent = new Intent(getActivity(), RecommendValidActivity.class);
                            if (o.equals("带访申请")){
                                intent.putExtra("type","0");
                            }else if (o.equals("复访申请")){
                                intent.putExtra("type","1");
                            }
                            startActivity(intent);
                        }
                    }
                });

                break;
            default:
                break;
        }
    }
}
